import Vue from 'vue'
import Main from './Main.vue'

let instance
const ImgPreviewCtor = Vue.extend(Main)

/**
 * 绑定到$utils上：this.$utils.preview(options)
 *
 * @typedef {{
 *  src: string
 *  thumb?: string
 *  caption?: string
 * }} ListOptions
 *
 * @typedef {{
 *  images: ListOptions[]
 *  visible?: boolean
 *  noText?: string
 *  index?: number
 *  showCaption?: boolean
 *  showFooter?: boolean
 *  showThumbs?: boolean
 *  nThumbs?: number
 * }} CfgOptions
 *
 * @param {ListOptions[]|CfgOptions} options
 */
const preview = (options = {}) => {
  if (Array.isArray(options)) {
    options = {
      images: options,
    }
  }
  if (options.visible === void 0) {
    options.visible = true
  }

  if (instance) {
    instance.$el.remove()
    instance.$destroy()
    instance = null
  }

  instance = new ImgPreviewCtor({
    propsData: options,
  })
  instance.$mount()
  document.body.appendChild(instance.$el)

  return instance
}

export default preview
